package com.augurit.smartform.database;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.stereotype.Component;

import com.augurit.common.util.lang.StringUtils;
import com.augurit.smartform.database.change.ColumnChangeInfo;
import com.augurit.smartform.database.change.ColumnCnNameChange;
import com.augurit.smartform.database.change.ColumnLengthChange;
import com.augurit.smartform.database.change.ColumnModifyInfo;
import com.augurit.smartform.database.change.ColumnNameChange;
import com.augurit.smartform.database.change.ColumnPrecisionChange;
import com.augurit.smartform.database.change.ColumnScaleChange;
import com.augurit.smartform.database.change.CopyDataInfo;
import com.augurit.smartform.database.change.DatabaseChange;
import com.augurit.smartform.database.change.DefaultValueChange;
import com.augurit.smartform.database.change.DictionaryChange;
import com.augurit.smartform.database.change.IsIndexChange;
import com.augurit.smartform.database.change.IsUniqueChange;
import com.augurit.smartform.database.change.NotNullChange;
import com.augurit.smartform.exception.FormException;

@Component
public class DatabaseComparator {
	
	/**
	 * 比较新的TABLE和数据库中的TABLE
	 * @param newTables
	 * @param oldTables
	 * @return
	 * cuigeqiang
	 */
	public DatabaseChange comparator(List<Table> newTables,List<Table> oldTables) throws FormException{
		DatabaseChange databaseChanges=new DatabaseChange();
		this.checkCreateTable(newTables, oldTables, databaseChanges);
		this.checkDropTable(newTables, oldTables, databaseChanges);
		this.checkAddColumn(newTables,oldTables,databaseChanges);
		this.checkDelColumn(newTables,oldTables,databaseChanges);
		this.checkModColumn(newTables, oldTables, databaseChanges);
		
		return databaseChanges;
	}
	
	/**
	 * 检查创建表
	 * @param newTables
	 * @param oldTables
	 * @param databaseChanges
	 * cuigeqiang
	 */
	private void checkCreateTable(List<Table> newTables,List<Table> oldTables,DatabaseChange databaseChanges) throws FormException{
		for(Table newTable : newTables){
			boolean creating=true;									//创建数据库表相关状态位
			List<Column> copyDataColumns=new ArrayList<Column>();	//需要COPY数据的列
			Table oldCopyTable=null;
			if(oldTables==null||oldTables.size()<=0){
				this.checkTableName(newTable.getName());
				databaseChanges.setCreateTables(newTable);
			}else{
				for(Table oldTable : oldTables){
					if(oldTable.getName().equalsIgnoreCase(newTable.getName())&&
							oldTable.getVersion()==newTable.getVersion()){
						creating=false;
						break;
					}else if(oldTable.getName().equalsIgnoreCase(newTable.getName())&&
							oldTable.getVersion()!=newTable.getVersion()){
						this.checkCopyDataColumn(newTable, oldTable, copyDataColumns);
						oldCopyTable=oldTable;
						break;
					}
				}
				if(creating){
					this.checkTableName(newTable.getName());
					databaseChanges.setCreateTables(newTable);
					if(copyDataColumns.size()>0){
						CopyDataInfo copyDataInfo=new CopyDataInfo();
						copyDataInfo.setNewTable(newTable);
						copyDataInfo.setOldTable(oldCopyTable);
						copyDataInfo.setColumns(copyDataColumns);
						databaseChanges.setCopyDataMap(newTable.getFullName(), copyDataInfo);
					}
				}
			}
		}
	}
	
	/**
	 * 检查删除表
	 * @param newTables
	 * @param oldTables
	 * @param databaseChanges
	 * cuigeqiang
	 */
	private void checkDropTable(List<Table> newTables,List<Table> oldTables,DatabaseChange databaseChanges){
		if(oldTables!=null&&oldTables.size()>0){
			for(Table oldTable : oldTables){
				if(newTables!=null&&newTables.size()>0){
					boolean dropping=true;
					for(Table newTable : newTables){
						if(oldTable.getName().equalsIgnoreCase(newTable.getName())){
							dropping=false;
							break;
						}
					}
					if(dropping){
						databaseChanges.setDropTables(oldTable);
					}
				}	
			}
		}
	}
	
	/**
	 * 检查新增字段信息
	 * @param newTables
	 * @param oldTables
	 * @param databaseChanges
	 * cuigeqiang
	 */
	private void checkAddColumn(List<Table> newTables,List<Table> oldTables,DatabaseChange databaseChanges){
		if(newTables!=null&&oldTables!=null&&newTables.size()>0&&oldTables.size()>0){
			for(Table newTable : newTables){
				Table oldListTable=null;
				if(!databaseChanges.getCreateTables().contains(newTable)){
					List<Column> newColumns=newTable.getColumn();
					for(Column newColumn : newColumns){
						boolean modBol=false;
						for(Table oldTable : oldTables){
							if(newTable.getName().equalsIgnoreCase(oldTable.getName())&&newTable.getVersion()==oldTable.getVersion()){
								oldListTable=oldTable;
								List<Column> oldColumns=oldTable.getColumn();
								for(Column oldColumn : oldColumns){
									if(newColumn.getFieldId()!=null){
										if(newColumn.getFieldId().equalsIgnoreCase(oldColumn.getFieldId())){
											modBol=true;
											break;
										}
									}else{									//主键或者外键							
										modBol=true;
										break;
									}
								}
								break;
							}
						}
						if(!modBol){
							if(oldListTable!=null){
								Map<String, ColumnChangeInfo> columnChangeInfos=databaseChanges.getColumnChangeInfos();
								ColumnChangeInfo columnChangeInfo=columnChangeInfos.get(newTable.getName());
								if(columnChangeInfo==null){
									ColumnChangeInfo newColumnChangeInfo=new ColumnChangeInfo();
									columnChangeInfos.put(newTable.getName(), newColumnChangeInfo);
									newColumnChangeInfo.setAddColumns(newColumn);
									newColumnChangeInfo.setNewTable(newTable);
									newColumnChangeInfo.setOldTable(oldListTable);
								}else{
									columnChangeInfo.setAddColumns(newColumn);
								}
							}
						}
					}
				}
			}
		}
	}
	
	/**
	 * 检查删除字段信息
	 * @param newTables
	 * @param oldTables
	 * @param databaseChanges
	 * cuigeqiang
	 */
	private void checkDelColumn(List<Table> newTables,List<Table> oldTables,DatabaseChange databaseChanges){
		if(newTables!=null&&oldTables!=null&&newTables.size()>0&&oldTables.size()>0){
			for(Table oldTable : oldTables){
				Table newListTable=null;
				if(!databaseChanges.getDropTables().contains(oldTable)){
					List<Column> oldColumns=oldTable.getColumn();
					for(Column oldColumn : oldColumns){
						boolean modBol=false;
						for(Table newTable : newTables){
							if(oldTable.getName().equalsIgnoreCase(newTable.getName())&&oldTable.getVersion()==newTable.getVersion()){
								newListTable=newTable;
								List<Column> newColumns=newTable.getColumn();
								for(Column newColumn : newColumns){
									if(oldColumn.getFieldId()!=null){
										if(oldColumn.getFieldId().equalsIgnoreCase(newColumn.getFieldId())){
											modBol=true;
											break;
										}
									}else{									//主键或者外键
										modBol=true;
										break;
									}
								}
								break;
							}
						}
						if(!modBol){
							if(newListTable!=null){
								Map<String, ColumnChangeInfo> columnChangeInfos=databaseChanges.getColumnChangeInfos();
								ColumnChangeInfo columnChangeInfo=columnChangeInfos.get(oldTable.getName());
								if(columnChangeInfo==null){
									ColumnChangeInfo newColumnChangeInfo=new ColumnChangeInfo();
									columnChangeInfos.put(oldTable.getName(), newColumnChangeInfo);
									newColumnChangeInfo.setDelColumns(oldColumn);
									newColumnChangeInfo.setNewTable(newListTable);
									newColumnChangeInfo.setOldTable(oldTable);
								}else{
									columnChangeInfo.setDelColumns(oldColumn);
								}
							}
						}
					}
				}	
			}
		}
	}
	
	/**
	 * 检查修改字段信息
	 * @param newTables
	 * @param oldTables
	 * @param databaseChanges
	 * cuigeqiang
	 */
	private void checkModColumn(List<Table> newTables,List<Table> oldTables,DatabaseChange databaseChanges) throws FormException{
		if(newTables!=null&&oldTables!=null&&newTables.size()>0&&oldTables.size()>0){
			for(Table newTable : newTables){
				if(!databaseChanges.getCreateTables().contains(newTable)){
					List<Column> newColumns=newTable.getColumn();
					for(Column newColumn : newColumns){
						for(Table oldTable : oldTables){
							if(newTable.getName().equalsIgnoreCase(oldTable.getName())&&newTable.getVersion()==oldTable.getVersion()){
								List<Column> oldColumns=oldTable.getColumn();
								for(Column oldColumn : oldColumns){
									if(newColumn.getFieldId()!=null){
										if(newColumn.getFieldId().equalsIgnoreCase(oldColumn.getFieldId())){
											this.compareColumnModifyInfo(newTable, oldTable, newColumn, oldColumn, databaseChanges);
											break;
										}
									}else{									//主键或者外键							
										break;
									}
								}
								break;
							}
						}
					}
				}
			}
		}
	}
	
	/**
	 * 在LIST中查询相同表名和版本号的表
	 * @param table
	 * @param tableList
	 * @return
	 * cuigeqiang
	 */
	private Table getTableByNameAndVersion(Table table,List<Table> tableList){
		Table returnTable=null;
		if(table!=null&&tableList!=null&&tableList.size()>0){
			for(Table listTable : tableList){
				if(table.getName().equals(listTable.getName())&&table.getVersion()==table.getVersion()){
					returnTable=listTable;
					break;
				}
			}
		}
		return returnTable;
	}
	
	/**
	 * 比较字段修改信息
	 * @param newTable
	 * @param oldTable
	 * @param newColumn
	 * @param oldColumn
	 * @param databaseChanges
	 * cuigeqiang
	 */
	private void compareColumnModifyInfo(Table newTable,Table oldTable,Column newColumn,Column oldColumn,DatabaseChange databaseChanges) throws FormException{
		if(newTable!=null&&oldTable!=null&&newColumn!=null&&oldColumn!=null&&databaseChanges!=null){
			Map<String, ColumnChangeInfo> columnChangeInfos=databaseChanges.getColumnChangeInfos();
			ColumnChangeInfo columnChangeInfo=columnChangeInfos.get(newTable.getName());
			if(columnChangeInfo==null){
				ColumnChangeInfo newColumnChangeInfo=new ColumnChangeInfo();
				columnChangeInfos.put(newTable.getName(), newColumnChangeInfo);
				
				newColumnChangeInfo.setNewTable(newTable);
				newColumnChangeInfo.setOldTable(oldTable);
				ColumnModifyInfo columnModifyInfo=new ColumnModifyInfo();
				newColumnChangeInfo.setModifyColumns(newColumn.getName(), columnModifyInfo);
				columnModifyInfo.setNewColumn(newColumn);
				columnModifyInfo.setOldColumn(oldColumn);
				
				this.setColumnModifyInfo(columnModifyInfo);
			}else{
				Map<String,ColumnModifyInfo> columnModifyInfos=columnChangeInfo.getModifyColumns();
				ColumnModifyInfo columnModifyInfo=columnModifyInfos.get(newColumn.getName());
				if(columnModifyInfo==null){
					ColumnModifyInfo newColumnModifyInfo=new ColumnModifyInfo();
					columnChangeInfo.setModifyColumns(newColumn.getName(), newColumnModifyInfo);
					newColumnModifyInfo.setNewColumn(newColumn);
					newColumnModifyInfo.setOldColumn(oldColumn);
					
					this.setColumnModifyInfo(newColumnModifyInfo);
				}else{
					this.setColumnModifyInfo(columnModifyInfo);
				}
			}
		}
	}
	
	/**
	 * 比较字段修改信息
	 * @param newColumn
	 * @param oldColumn
	 * @param columnModifyInfo
	 * cuigeqiang
	 */
	private void setColumnModifyInfo(ColumnModifyInfo columnModifyInfo) throws FormException{
		if(columnModifyInfo!=null){
			Column newColumn=columnModifyInfo.getNewColumn();
			Column oldColumn=columnModifyInfo.getOldColumn();
			if(newColumn!=null&&oldColumn!=null){
				this.checkColumnTypeChange(newColumn, oldColumn);
				this.checkColumnNameChange(newColumn, oldColumn, columnModifyInfo);
				this.checkColumnLengthChange(newColumn, oldColumn, columnModifyInfo);
				this.checkColumnPrecisionChange(newColumn, oldColumn, columnModifyInfo);
				this.checkColumnScaleChange(newColumn, oldColumn, columnModifyInfo);
				this.checkNotNullChange(newColumn, oldColumn, columnModifyInfo);
				this.checkDefaultValueChange(newColumn, oldColumn, columnModifyInfo);
				this.checkDictionaryChange(newColumn, oldColumn, columnModifyInfo);
				this.checkUniqueChange(newColumn, oldColumn, columnModifyInfo);
				this.checkIndexChange(newColumn, oldColumn, columnModifyInfo);
				this.checkColumnCnNameChange(newColumn, oldColumn, columnModifyInfo);
			}
		}
	}
	
	/**
	 * 检查是否改变了SQLTYPE
	 * @param newColumn
	 * @param oldColumn
	 * cuigeqiang
	 */
	private void checkColumnTypeChange(Column newColumn,Column oldColumn) throws FormException{
		if(!newColumn.getSqlType().equalsIgnoreCase(oldColumn.getSqlType())){
			throw new FormException("不允许修改数据类型");
		}
	}
	
	/**
	 * 检查字段名变化
	 * @param newColumn
	 * @param oldColumn
	 * cuigeqiang
	 */
	private void checkColumnNameChange(Column newColumn,Column oldColumn,ColumnModifyInfo columnModifyInfo){
		if(!newColumn.getName().equalsIgnoreCase(oldColumn.getName())){
			ColumnNameChange columnNameChange=new ColumnNameChange();
			columnModifyInfo.setColumnNameChange(columnNameChange);
			columnNameChange.setNewName(newColumn.getName());
			columnNameChange.setOldName(oldColumn.getName());
		}
	}
	
	/**
	 * 检查字段长度变化
	 * @param newColumn
	 * @param oldColumn
	 * @param columnModifyInfo
	 * cuigeqiang
	 */
	private void checkColumnLengthChange(Column newColumn,Column oldColumn,ColumnModifyInfo columnModifyInfo) throws FormException{
		
		if(newColumn.getLength()!=null&&oldColumn.getLength()!=null){
			if(newColumn.getLength().intValue()<oldColumn.getLength().intValue()){
				throw new FormException("字段"+newColumn.getName()+"数据长度不允许调小");
			}else if(newColumn.getLength().intValue()>oldColumn.getLength().intValue()){
				ColumnLengthChange columnLengthChange=new ColumnLengthChange();
				columnModifyInfo.setColumnLengthChange(columnLengthChange);
				columnLengthChange.setNewLength(newColumn.getLength());
				columnLengthChange.setOldLength(oldColumn.getLength());
			}
		}
	}
	
	/**
	 * 检查字段的精度
	 * @param newColumn
	 * @param oldColumn
	 * @param columnModifyInfo
	 * cuigeqiang
	 */
	private void checkColumnPrecisionChange(Column newColumn,Column oldColumn,ColumnModifyInfo columnModifyInfo) throws FormException{
		Integer newPrecision=newColumn.getPrecision();
		Integer oldPrecision=oldColumn.getPrecision();
		if(newPrecision!=null&&oldPrecision!=null){
			if(newPrecision.intValue()<oldPrecision.intValue()){
				throw new FormException("字段"+newColumn.getName()+"数据精度不允许调小");
			}else if(newPrecision.intValue()>oldPrecision.intValue()){
				ColumnPrecisionChange columnPrecisionChange=new ColumnPrecisionChange();
				columnModifyInfo.setColumnPrecisionChange(columnPrecisionChange);
				columnPrecisionChange.setNewPresion(newPrecision);
				columnPrecisionChange.setOldPresion(oldPrecision);
			}
		}
	}
	
	/**
	 * 检查小数点位数
	 * @param newColumn
	 * @param oldColumn
	 * @param columnModifyInfo
	 * cuigeqiang
	 */
	private void checkColumnScaleChange(Column newColumn,Column oldColumn,ColumnModifyInfo columnModifyInfo) throws FormException{
		Integer newScale=newColumn.getScale();
		Integer oldScale=oldColumn.getScale();
		if(newScale!=null&&oldScale!=null){
			if(newScale.intValue()<oldScale.intValue()){
				throw new FormException("字段"+newColumn.getName()+"数据标度不允许调小");
			}else if(newScale.intValue()>oldScale.intValue()){
				ColumnScaleChange columnScaleChange=new ColumnScaleChange();
				columnModifyInfo.setColumnScaleChange(columnScaleChange);
				columnScaleChange.setNewScale(newScale);
				columnScaleChange.setOldScale(oldScale);
			}
		}
	}
	
	/**
	 * 检查是否为空字段
	 * @param newColumn
	 * @param oldColumn
	 * @param columnModifyInfo
	 * cuigeqiang
	 */
	private void checkNotNullChange(Column newColumn,Column oldColumn,ColumnModifyInfo columnModifyInfo) throws FormException{
		boolean newNotNull=newColumn.isNotNull();
		boolean oldNotNull=oldColumn.isNotNull();
		if(newNotNull==true&&oldNotNull==false){
			throw new FormException("字段"+newColumn.getName()+"已经允许为空，不允许修改为不为空");
		}else if(newNotNull==false&&oldNotNull==true){
			NotNullChange notNullChange=new NotNullChange();
			columnModifyInfo.setNotNullChange(notNullChange);
			notNullChange.setNewNotNull(newNotNull);
			notNullChange.setOldNotNull(oldNotNull);
		}
	}
	
	/**
	 * 检查是否改变了默认值
	 * @param newColumn
	 * @param oldColumn
	 * @param columnModifyInfo
	 * cuigeqiang
	 */
	private void checkDefaultValueChange(Column newColumn,Column oldColumn,ColumnModifyInfo columnModifyInfo){
		String newDefaultValue=newColumn.getDefaultValue();
		String oldDefaultValue=oldColumn.getDefaultValue();
		if(StringUtils.isBlank(newDefaultValue)&&StringUtils.isBlank(oldDefaultValue)){
			
		}else if(StringUtils.isBlank(newDefaultValue)&&StringUtils.isNotBlank(oldDefaultValue)){
			DefaultValueChange defaultValueChange=new DefaultValueChange();
			columnModifyInfo.setDefaultValueChange(defaultValueChange);
			defaultValueChange.setNewDefaultValue(null);
			defaultValueChange.setNewSqlDefaultValue(null);
			defaultValueChange.setOldDefaultValue(oldDefaultValue);
			defaultValueChange.setOldSqlDefaultValue(oldColumn.getSqlDefaultValue());
		}else{
			if(!newDefaultValue.equalsIgnoreCase(oldDefaultValue)){
				DefaultValueChange defaultValueChange=new DefaultValueChange();
				columnModifyInfo.setDefaultValueChange(defaultValueChange);
				defaultValueChange.setNewDefaultValue(newDefaultValue);
				defaultValueChange.setNewSqlDefaultValue(newColumn.getSqlDefaultValue());
				defaultValueChange.setOldDefaultValue(oldDefaultValue);
				defaultValueChange.setOldSqlDefaultValue(oldColumn.getSqlDefaultValue());
			}
		}
	}
	
	/**
	 * 是否改变了数据字典
	 * @param newColumn
	 * @param oldColumn
	 * @param columnModifyInfo
	 * cuigeqiang
	 */
	private void checkDictionaryChange(Column newColumn,Column oldColumn,ColumnModifyInfo columnModifyInfo){
		String newDictionary=newColumn.getDictionary();
		String oldDictionary=oldColumn.getDictionary();
		if(StringUtils.isNotBlank(newDictionary)&&StringUtils.isNotBlank(oldDictionary)){
			if(!newDictionary.equalsIgnoreCase(oldDictionary)){
				DictionaryChange dictionaryChange=new DictionaryChange();
				columnModifyInfo.setDictionaryChange(dictionaryChange);
				dictionaryChange.setNewDictionaryRef(newDictionary);
				dictionaryChange.setOldDictionaryRef(oldDictionary);
			}
		}
	}
	
	/**
	 * 是否改变了字段的中文名
	 * @param newColumn
	 * @param oldColumn
	 * @param columnModifyInfo
	 * cuigeqiang
	 */
	private void checkColumnCnNameChange(Column newColumn,Column oldColumn,ColumnModifyInfo columnModifyInfo){
		String newCnName=newColumn.getCnName();
		String oldCnName=oldColumn.getCnName();
		if(StringUtils.isNotBlank(newCnName)&&StringUtils.isNotBlank(oldCnName)){
			if(!newCnName.equalsIgnoreCase(oldCnName)){
				ColumnCnNameChange columnCnNameChange=new ColumnCnNameChange();
				columnModifyInfo.setColumnCnNameChange(columnCnNameChange);
				columnCnNameChange.setNewName(newCnName);
				columnCnNameChange.setOldName(oldCnName);
			}
		}
	}
	
	/**
	 * 检查唯一性约束是否改变
	 * @param newColumn
	 * @param oldColumn
	 * @param columnModifyInfo
	 * cuigeqiang
	 */
	private void checkUniqueChange(Column newColumn,Column oldColumn,ColumnModifyInfo columnModifyInfo){
		boolean newUnique=newColumn.isUnique();
		boolean oldUnique=oldColumn.isUnique();
		if(newUnique!=oldUnique){
			IsUniqueChange uniqueChange=new IsUniqueChange();
			columnModifyInfo.setIsUniqueChange(uniqueChange);
			uniqueChange.setNewUnique(newUnique);
			uniqueChange.setOldUnique(oldUnique);
		}
	}
	
	/**
	 * 检查索引是否改变
	 * @param newColumn
	 * @param oldColumn
	 * @param columnModifyInfo
	 * cuigeqiang
	 */
	private void checkIndexChange(Column newColumn,Column oldColumn,ColumnModifyInfo columnModifyInfo){
		boolean newIdx=newColumn.isIndex();
		boolean oldIdx=oldColumn.isIndex();
		if(newIdx!=oldIdx){
			IsIndexChange indexChange=new IsIndexChange();
			columnModifyInfo.setIsIndexChange(indexChange);
			indexChange.setNewIndex(newIdx);
			indexChange.setOldIndex(oldIdx);
		}
	}
	
	/**
	 * 判断表名是否合法
	 * @param tableName
	 * @return
	 * cuigeqiang
	 */
	private void checkTableName(String tableName) throws FormException{
		if(tableName.length()>10){
			throw new FormException("表单名称必须不能大于10个字符");
		}else{
			Pattern pattern = Pattern.compile("^[A-Za-z][A-Za-z0-9_$#]*$");
			Matcher matcher=pattern.matcher(tableName);
			if(!matcher.find()){
				throw new FormException("表单名称只能包含“字母数字_$#”字符并且只能以字母开头");
			}
		}
	}
	
	/**
	 * 检查数据库表需要拷贝数据的列
	 * @param newTable
	 * @param oldTable
	 * @param copyDataColumn
	 * cuigeqiang
	 */
	private void checkCopyDataColumn(Table newTable,Table oldTable,List<Column> copyDataColumn){
		if(newTable!=null&&oldTable!=null&&copyDataColumn!=null){
			List<Column> newColumns=newTable.getColumn();
			for(Column newColumn : newColumns){
				boolean flag=false;
				List<Column> oldColumns=oldTable.getColumn();
				for(Column oldColumn : oldColumns){
					if(newColumn.getFieldId()!=null){
						if(newColumn.getFieldId().equalsIgnoreCase(oldColumn.getFieldId())
								&&newColumn.getName().equalsIgnoreCase(oldColumn.getName())
								&&newColumn.getSqlType().equalsIgnoreCase(oldColumn.getSqlType())
								&&newColumn.isNotNull()==oldColumn.isNotNull()
								&&newColumn.isUnique()==oldColumn.isUnique()
								&&(newColumn.getLength()==null&&oldColumn.getLength()==null||newColumn.getLength()!=null&&oldColumn.getLength()!=null&&newColumn.getLength().compareTo(oldColumn.getLength())>=0)
								&&(newColumn.getPrecision()==null&&oldColumn.getPrecision()==null||newColumn.getPrecision()!=null&&oldColumn.getPrecision()!=null&&newColumn.getPrecision().compareTo(oldColumn.getPrecision())>=0)
								&&(newColumn.getScale()==null&&oldColumn.getScale()==null||newColumn.getScale()!=null&&oldColumn.getScale()!=null&&newColumn.getScale().compareTo(oldColumn.getScale())>=0)
						){
							flag=true;		
							break;
						}
					}else{									//主键或者外键							
						if(newColumn.isPrimaryKey()){
							flag=true;
						}
						break;
					}
				}
				if(flag){
					copyDataColumn.add(newColumn);
				}
			}
		}
	}
}
